//
//  MOMyGetMarriedOfHisViewController.m
//  MobileOffice
//
//  Created by 冰冰 on 2019/8/16.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MOMyGetMarriedOfHisViewController.h"
#import "MOMyGetMarriedHisEditOrCreatViewController.h"
#import "MOMyGetMarriedPlanEditOrCreatViewController.h"

@interface MOMyGetMarriedOfHisViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataSource;

@end

@implementation MOMyGetMarriedOfHisViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initNav];
    
    [self creatView];
    
    [self initData];
}

-(void)initNav
{
    UIButton *addButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 40)];
    [addButton setImage:IMAGE(@"use_add") forState:UIControlStateNormal];
    [addButton addTarget:self action:@selector(clickAddMarried) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc]initWithCustomView:addButton];
    self.navigationItem.rightBarButtonItem = rightButtonItem;
}

-(void)creatView
{
    self.title = @"结亲记录";
    
    self.view.backgroundColor = KGrayColor_ededed;
    
    [self.view addSubview:self.tableView];
    
}

-(void)initData
{
    NSString *url = [NSString stringWithFormat:@"%@?authorization=%@",RECIPIENTRECORD_GETRECIPIENTRECORDS_URL,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [PPNetworkHelper POST:url parameters:@{@"masterId":self.masterId} success:^(id responseObject) {
        NSLog(@"结亲记录列表：%@",responseObject);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if ([responseObject isKindOfClass:[NSArray class]]) {
            NSArray *arr = responseObject;
            self.dataSource = [NSMutableArray array];
            for (int i = 0; i<arr.count; i++) {
                [self.dataSource addObject:arr[i]];
            }
            [self.tableView reloadData];
        }
    } failure:^(NSError *error) {
        NSLog(@"结亲记录列表：%@",[error localizedDescription]);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [[ADShowMessage shareShowMessage]showTextOnly:[error localizedDescription] showInView:self.view];
    }];
}


#pragma mark -- 添加结亲记录
-(void)clickAddMarried
{
    NSString *url = [NSString stringWithFormat:@"%@?authorization=%@",RECIPIENTPROJECT_GETECIPIENTPROJECTBYMASTERID_URL,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [PPNetworkHelper POST:url parameters:@{@"masterId":self.masterId} success:^(id responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            if ([[NSString passNullForString:responseObject[@"id"]] integerValue]>0) {
                MOMyGetMarriedHisEditOrCreatViewController *vc = [[MOMyGetMarriedHisEditOrCreatViewController alloc]init];
                vc.editOrCreatStatus = @"2";
                __weak typeof (self)weakSelf = self;
                vc.editOrCreatActionBlock = ^{
                    [weakSelf initData];
                };
                vc.masterId = self.masterId;
                [self.navigationController pushViewController:vc animated:YES];
            }else{
                [self showSelected];
            }
        }else{
            [self showSelected];
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [self showSelected];
    }];
    
    
}


-(void)showSelected
{
    MOHintView *hintView = [[MOHintView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) andContentStr:@"您还未填写结亲计划，是否现在填写？"];
    __weak typeof (self)weakSelf = self;
    hintView.clickSureButtonBlock = ^{
        MOMyGetMarriedPlanEditOrCreatViewController *vc = [[MOMyGetMarriedPlanEditOrCreatViewController alloc]init];
        vc.masterId = weakSelf.masterId;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    };
    [WINDOW addSubview:hintView];
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.textColor = KBlackColor_222222;
        cell.textLabel.font = FONT_SIZE(15);
    }
    NSString *str = [self.dataSource[indexPath.row][@"createTime"] substringWithRange:NSMakeRange(0, 10)];
    cell.textLabel.text = [NSString stringWithFormat:@"%@\t第(%@)次",str,self.dataSource[indexPath.row][@"batch"]];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kHConversions(54);
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 13;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 13;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MOMyGetMarriedHisEditOrCreatViewController *vc = [[MOMyGetMarriedHisEditOrCreatViewController alloc]init];
    vc.editOrCreatStatus = @"1";
    vc.idStr = self.dataSource[indexPath.row][@"id"];
    __weak typeof (self)weakSelf = self;
    vc.editOrCreatActionBlock = ^{
        [weakSelf initData];
    };
    vc.masterId = self.masterId;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 设置顶头分割线
-(void)viewDidLayoutSubviews {
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])  {
        if (@available(iOS 8.0, *)) {
            [self.tableView setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
        } else {
            // Fallback on earlier versions
        }
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        if (@available(iOS 8.0, *)) {
            [cell setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
        } else {
            // Fallback on earlier versions
        }
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
}


#pragma mark -懒加载
-(UITableView *)tableView
{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-StatusBarAndNavigationBarHeight - TabbarSafeBottomMargin) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = KGrayColor_ededed;
        _tableView.showsVerticalScrollIndicator = NO;
        
    }
    return _tableView;
}

@end
